/**
 * Copyright (C) 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gjc.server.example;

import java.io.BufferedReader;
import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.gjc.client.example.ApiCommandPatternTestGwt;

/**
 * Servlet component
 * for mock testing of example...
 * @author <a href="mailto:mail@raphaelbauer.com">rEyez</<a>
 */
public class ApiMockServlet extends HttpServlet {

    @Override
    protected void doDelete(HttpServletRequest request,
            HttpServletResponse response) {

        try {
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().print(
                    ApiCommandPatternTestGwt.SERVLET_DELETE_RESPONSE);
        } catch (IOException e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws IOException {

        String pathInfo = request.getPathInfo();
        response.getWriter().print(ApiCommandPatternTestGwt.exampleResponse);

    }

    @Override
    protected void doHead(HttpServletRequest request,
            HttpServletResponse response) {
        response.setStatus(HttpServletResponse.SC_OK);
    }

    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws IOException {



    	response.setCharacterEncoding("UTF-8");

        String pathInfo = request.getPathInfo();
        System.out.println("in POST of Mockservlet" + pathInfo);

        response.getWriter().print(ApiCommandPatternTestGwt.exampleResponse);


    }

    @Override
    protected void doPut(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        BufferedReader reader = request.getReader();
        String content = reader.readLine();
        if (content != null
                && content.equals("<html><body>Put Me</body></html>")) {
            response.getWriter().print(
                    ApiCommandPatternTestGwt.SERVLET_PUT_RESPONSE);
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }
}